package com.odianyun.hello.web.controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.odianyun.hello.model.DTO.CodeDTO;
import com.odianyun.hello.model.VO.CodeVO;
import com.odianyun.hello.service.CodeService;
import com.odianyun.project.query.PageQueryArgs;
import com.odianyun.project.query.QueryArgs;
import com.odianyun.project.model.vo.ObjectResult;
import com.odianyun.project.model.vo.PageResult;
import com.odianyun.project.model.vo.Result;
import com.odianyun.project.support.base.controller.BaseController;
import com.odianyun.db.query.PageVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import java.util.*;

@Api(value="CodeController", tags="Code")
@RequestMapping("code")
@RestController
public abstract class AbstractCodeController<DTO extends CodeDTO> extends BaseController {
    @Resource
    protected CodeService service;

	@ApiOperation(value="Code-分页查询")
	@PostMapping("/listPage")
    public PageResult<CodeVO> listPage(@RequestBody @ApiParam(value="查询入参，如果参数中有中括号则需要转义") PageQueryArgs args) {
        PageVO<CodeVO> pageVO = service.listPage(args);
        return PageResult.ok(pageVO);
    }

    @ApiOperation("Code-查询")
    @GetMapping("/getById")
    public ObjectResult<CodeVO> getById(@RequestParam("id") Long id) {
        CodeVO vo = service.getById(id);

        return ObjectResult.ok(vo);
    }

    @ApiOperation("Code-添加")
    @PostMapping("/add")
    public ObjectResult<Long> add(@RequestBody @Valid DTO u) throws Exception {
        notNull(u);

        return ObjectResult.ok(service.addWithTx(u));
    }

    @ApiOperation("Code-修改")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid DTO u) throws Exception {
        notNull(u);
        fieldNotNull(u, "id");

        service.updateWithTx(u);
        return Result.OK;
    }

    @ApiOperation("Code-删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) throws Exception {
        notNull(ids);

        service.deletesWithTx(ids);
        return Result.OK;
    }
	
}
